package com.jxwifi.cloud.quickclean.popwindow;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.idroid.utils.NoDoubleClickListener;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.bean.RecordsListBean;
import com.jxwifi.cloud.quickclean.bean.StrListBean;
import com.popwindow.BasicPopmodule;

import java.util.ArrayList;
import java.util.List;

public class PopShopDetails extends BasicPopmodule {

    private RecordsListBean mRecords;

    public PopShopDetails(Context context, RecordsListBean list) {
        this.mContext = context;
        this.mRecords = list;
        initView();
    }

    @Override
    public void setPopwindow(PopupWindow pop) {
        mPopwindows = pop;
    }

    @Override
    public View getView() {
        return mContentView;
    }

    private void initView() {
        mContentView = LayoutInflater.from(mContext).inflate(R.layout.pop_shop_details, new LinearLayout(mContext), false);
        setListener();
    }

    public void setListener() {
        View mVwPopBlank = (View) mContentView.findViewById(R.id.vw_pop_blank);
        ImageView mImgPopShopDetails = (ImageView) mContentView.findViewById(R.id.img_pop_shop_details);
        TextView mTvPopShopDetailsTitle = (TextView) mContentView.findViewById(R.id.tv_pop_shop_details_title);
        WebView mWvProductDetails = (WebView) mContentView.findViewById(R.id.wv_product_details);
        TextView mTvToolKit = (TextView) mContentView.findViewById(R.id.tv_tool_kit_items_specifications);

        mTvPopShopDetailsTitle.setText(mRecords.getName());
        if (!TextUtils.isEmpty(mRecords.getJsonStr())) {
            List<StrListBean> mList = new ArrayList<>();
            mList = JSON.parseArray(mRecords.getJsonStr(), StrListBean.class);
            String Specifications = "";
            for (int i = 0; i < mList.size(); i++) {
                Specifications = Specifications + "" + mList.get(i).getKey() + ":" + mList.get(i).getValue();
            }
            mTvToolKit.setText("规格：" + Specifications);
        } else {
            mTvToolKit.setText("规格：暂无");
        }

        mWvProductDetails.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        WebSettings webSettings = mWvProductDetails.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH);
        webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        String DetailContent = mRecords.getAttachNote().replace("width=", "")
                .replace("<img", "<img style=\"width:100%;height:auto;margin:0;\"")
                .replace("<p", "<p style=\"font-size:36px;\"");
        mWvProductDetails.loadDataWithBaseURL(null, DetailContent, "text/html", "UTF-8", null);

        Glide.with(mContext).load(mRecords.getImgUrl()).error(R.mipmap.icon_default_head).into(mImgPopShopDetails);
        mVwPopBlank.setOnClickListener(new NoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                mPopwindows.dismiss();
            }
        });
    }
}
